********************************************************************************
********************************************************************************
** Robustness analysis 8
** Heterogeneity in regard to peer quality
********************************************************************************
********************************************************************************

** Load data
use "$dataraw_path\data_ftna_publication.dta", clear


** Separate performance of peers by a student's own and opposite gender

* Number of schoolmates with same gender as student i and their psle scores
bys school_id year female: egen temp_number1 = count(gpa_psle_core)
bys school_id year female: egen temp_score1 = mean(gpa_psle_core)
gen peers1 = (temp_score1*temp_number1 - gpa_psle_core) / (temp_number1 - 1)

* Number of male schoolmates and their psle scores
bys school_id year: egen temp_number2 = count(gpa_psle_core) if female==0
bys school_id year: egen temp_score2 = mean(gpa_psle_core) if female==0
gen temp_average2 = (temp_score2*temp_number2) / temp_number2
bys school_id year: egen temp_peers_boys = max(temp_average2)

* Number of female schoolmates and their psle scores
bys school_id year: egen temp_number3 = count(gpa_psle_core) if female==1
bys school_id year: egen temp_score3 = mean(gpa_psle_core) if female==1
gen temp_average3 = (temp_score3*temp_number3) / temp_number3
bys school_id year: egen temp_peers_girls = max(temp_average3)

* Average psle scores of female schoolmates for boys, and average psle scores
* of male schoolmates for girls
gen peers2 = temp_peers_boys if female==1
replace peers2 = temp_peers_girls if female==0

* Standardize the scores of peers
egen peers1_sd = std(peers1) if sample==1
egen peers2_sd = std(peers2) if sample==1

* For peers1_sd (own gender peers) give zero to students that are the only ones
* in their school being their gender. Otherwise they will be excluded when
* dividing with zero. Similarly, for peers2_sd (opposite gender peers) give zero
* to students with no opposite gender schoolmates. The peer variables are
* interacted with "no_peers_own_gender" and "no_peers_opp_gender" as students
* being the only ones with their gender and students with no opposite gender
* schoolmates should have no effect on the peer variables. They are just given
* a value of zero in order not to be excluded from the sample.
gen no_peers_own_gender = (temp_number1==1) if sample==1
gen no_peers_opp_gender = (temp_peers_boys==. | temp_peers_girls==.) if sample==1

replace peers1_sd = 0 if no_peers_own_gender==1
replace peers2_sd = 0 if no_peers_opp_gender==1

drop temp*


** Interaction with non-separated peer quality
eststo rob8_1: areg gpa_ftna_core_sd private##c.peers_score_core_sd ///
	female uncommon_name peers_fail_share peers_as_share gpa_psle_other_sd, ///
	cl(school_id) a(group_id)


** Interactions with gender-separated peer quality (own and opposite gender)
eststo rob8_2: areg gpa_ftna_core_sd 1.private female uncommon_name ///
	peers1_sd peers2_sd 1.private#c.peers1_sd 1.private#c.peers2_sd ///
	peers_fail_share peers_as_share gpa_psle_other_sd ///
	if sample==1 & mix_school==1, cl(school_id) a(group_id)

** Drop additionally created variables
drop peers1-no_peers_opp_gender


** Output
esttab rob8_1 rob8_2 using "$out_path\tableb4.tex", replace se ///
stats(N r2, fmt(%12.3gc) labels("\(N\)" "\(R^2\)")) compress nomtitles ///
starlevels("" 0.01) substitute(\_ _) b(3) ///
/*KEEP*/k(1.private peers_score_core_sd 1.private#c.peers_score_core_sd ///
peers1_sd 1.private#c.peers1_sd peers2_sd 1.private#c.peers2_sd) ///
/*ORDER*/o(1.private peers_score_core_sd 1.private#c.peers_score_core_sd ///
peers1_sd 1.private#c.peers1_sd peers2_sd 1.private#c.peers2_sd) ///
/*LABELS*/varl(1.private "\$Private_s$" ///
peers_score_core_sd "\$\textit{Peers PSLE}_{s}$" ///
1.private#c.peers_score_core_sd "\$Private_s \times \textit{Peers PSLE}_{s}$" ///
peers1_sd "\$\textit{Peers PSLE own}_{s}$" ///
1.private#c.peers1_sd "\$Private_s \times \textit{Peers PSLE own}_{s}$" ///
peers2_sd "\$\textit{Peers PSLE opposite}_{s}$" ///
1.private#c.peers2_sd "\$Private_s \times \textit{Peers PSLE opposite}_{s}$")
